<?php


require_once '../../../config.php';
require_once '../../../functions.php';

$substance = book_fetch_all("select 
list.title,
options.option_isbn
from options
inner join list on list.list_isbn = options.option_isbn");


// function add()
// {
//     $description = $_POST['description'];
//     $authors = $_POST['authors'];
//     $link = $_POST['link'];
//     $rows = book_execute("insert into options (id,description,authors,link) values (null,'{$description}','{$authors}','{$link}')");
//     $GLOBALS['success'] = $rows > 0;
//     $GLOBALS['success'] = $rows <= 0 ? '更新成功！':'更新失败';
// }

function edit()
{
    global $content;
    print($content);
    $isbn = $content['option_isbn'];
    var_dump($isbn);
    $description = empty($_POST['description'])?$content['description']:$_POST['description'];

    $authors = empty($_POST['authors'])?$content['authors']:$_POST['authors'];

    $link = empty($_POST['link'])?$content['link']:$_POST['link'];

    $rows = book_execute("update options set description='{$description}',authors='{$authors}',link='{$link}' where option_isbn='{$isbn}'");
   
    

    $GLOBALS['success'] = $rows > 0;
    $GLOBALS['success'] = $rows <= 0 ? '更新成功！':'更新失败';
}
var_dump($_GET['isbn']);
if (empty($_GET['isbn'])) {
    if ($_SERVER['REQUEST_METHOD']==='POST') {
        var_dump($_GET['isbn']);
        // add();
    }
} else {
    $content= book_fetch_one('select
  list.title,
  options.description,
  options.authors,
  options.link,
  options.option_isbn
  from options
  inner join list on list.list_isbn = options.option_isbn
  where options.option_isbn ='.$_GET['isbn']);
    if ($_SERVER['REQUEST_METHOD']==='POST') {
        edit();
    }
}




?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Users &laquo; Admin</title>
  <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/static/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="/static/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="/static/assets/css/admin.css">
  <script src="/static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
  <script>NProgress.start()</script>

  <div class="main">
   

    <div class="container-fluid">
      <div class="page-title">
        <h1>内容修改</h1>
      </div>
      <?php if (isset($message)): ?>
     <?php if ($success):?>
              <!-- 有错误信息时展示 -->
      <div class="alert alert-success">
        <strong>成功！</strong><?php echo $message;?>
      </div>
    <?php else:?>
       <div class="alert alert-danger">
        <strong>错误！</strong><?php echo $message;?>
      </div>
     <?php endif ?>
     <?php endif ?>
      <form class="form-inline" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
          <select name="isbn" class="form-control input-sm">
            <option value="all">所有分类</option>
            <?php foreach ($substance as $item): ?>
            <option value="<?php echo $item['option_isbn']; ?>"<?php echo isset($_GET['isbn']) && $_GET['isbn'] == $item['option_isbn'] ? ' selected' : '' ?>>
              <?php echo $item['title']; ?>
            </option>
            <?php endforeach ?>
          </select>
          <button class="btn btn-default btn-sm">筛选</button>
        </form>
       
        <div class="col-md-6">
        <?php if (isset($content)):?>
           
            <form action="<?php echo $_SERVER['PHP_SELF'];?>?isbn=<?php echo $content['option_isbn']; ?>" method="post">
            <div class="form-group">
             
            </div>
            <div class="form-group">
              <label for="description">描述</label>
              <textarea class="form-control" name="description" id="description" cols="30" rows="10" placeholder="描述"><?php echo $content['description'] ?></textarea>
            </div>

            <div class="form-group">
              <label for="authors">作者</label>
              <textarea class="form-control" name="authors" id="authors" cols="30" rows="10" placeholder="作者"><?php echo $content['authors'] ?></textarea>
            </div>
            <div class="form-group">
              <label for="link">链接</label>
              <input class="form-control" type="text" name="link" id="link" value="<?php echo $content['link'] ?>">
            </div>
            <div class="form-group">
              <button class="btn btn-info" type="submit">保存</button>
              <a href="/admin/book.php" class="btn btn-info" type="submit">返回</a>
            </div>
          </form>
        <?php else:?>
            <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
            <div class="form-group">
             
            </div>
            <div class="form-group">
              <label for="description">描述</label>
              <textarea class="form-control" name="description" id="description" cols="30" rows="10" placeholder="描述"></textarea>
            </div>
            <div class="form-group">
              <label for="authors">作者</label>
              <textarea class="form-control" name="authors" id="authors" cols="30" rows="10" placeholder="作者"></textarea>
            </div>
            <div class="form-group">
              <label for="link">链接</label>
              <input class="form-control" type="text" name="link" id="link" value="">
            </div>
            <div class="form-group">
              <button class="btn btn-info" type="submit">添加</button>
              <a href="/admin/book.php" class="btn btn-info" type="submit">返回</a>
            </div>
          </form>
        <?php endif?>
          
         
        </div>
    </div>
  </div>

  <?php $current_page = 'book'; ?>


  <script src="/static/assets/vendors/jquery/jquery.js"></script>
  <script src="/static/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script>NProgress.done()</script>
</body>
</html>
